package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private JedisPool pool;
    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        Page<Address> page = addressDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<String> findAll() {
        return addressDao.findAll();
    }

    @Override
    public Address findById(Integer id) {

        return addressDao.findById(id);
    }

    @Override
    public List<Map> getSetmealReport() {
        return null;
    }

    @Override
    public void add(Address address) {
        addressDao.add(address);
        //完成数据库操作后需要将图片名称保存到redis
        pool.getResource().sadd(RedisConstant.ADDRESS_PIC_DB_RESOURCES,address.getImg());
    }

    @Override
    public void edit(Address address) {
        addressDao.update(address);
        //完成数据库操作后需要将图片名称保存到redis
        pool.getResource().sadd(RedisConstant.ADDRESS_PIC_DB_RESOURCES,address.getImg());
    }

    //删除地址信息
    @Override
    public void delete(Integer id) {
        //删除依赖
        addressDao.deleteOrderAndAddressByAddressId(id);
        addressDao.deleteAddressById(id);
    }
}
